/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import entities.Project;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import java.util.List;
import entities.ProjColumn;

/**
 *
 * @author Alex
 */

@XmlRootElement(name = "project")
public class ProjectConverter {
    private Project entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of ProjectConverter */
    public ProjectConverter() {
        entity = new Project();
    }

    /**
     * Creates a new instance of ProjectConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public ProjectConverter(Project entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getProjId() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getProjColumnList();
    }

    /**
     * Creates a new instance of ProjectConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public ProjectConverter(Project entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for projId.
     *
     * @return value for projId
     */
    @XmlElement
    public Long getProjId() {
        return (expandLevel > 0) ? entity.getProjId() : null;
    }

    /**
     * Setter for projId.
     *
     * @param value the value to set
     */
    public void setProjId(Long value) {
        entity.setProjId(value);
    }

    @XmlElement
    public Long getIterationLen() {
        return (expandLevel > 0) ? entity.getIterationLen() : null;
    }
    
    public void setIterationLen(Long value) {
        entity.setIterationLen(value);
    }

    @XmlElement
    public Date getIterationStart() {
        return (expandLevel > 0) ? entity.getIterationStart() : null;
    }

    public void setIterationStart(Date value) {
        entity.setIterationStart(value);
    }
    /**
     * Getter for projName.
     *
     * @return value for projName
     */
    @XmlElement
    public String getProjName() {
        return (expandLevel > 0) ? entity.getProjName() : null;
    }

    /**
     * Setter for projName.
     *
     * @param value the value to set
     */
    public void setProjName(String value) {
        entity.setProjName(value);
    }

    /**
     * Getter for description.
     *
     * @return value for description
     */
    @XmlElement
    public String getDescription() {
        return (expandLevel > 0) ? entity.getDescription() : null;
    }

    /**
     * Setter for description.
     *
     * @param value the value to set
     */
    public void setDescription(String value) {
        entity.setDescription(value);
    }

    /**
     * Getter for startDate.
     *
     * @return value for startDate
     */
    @XmlElement
    public Date getStartDate() {
        return (expandLevel > 0) ? entity.getStartDate() : null;
    }

    /**
     * Setter for startDate.
     *
     * @param value the value to set
     */
    public void setStartDate(Date value) {
        entity.setStartDate(value);
    }

    /**
     * Getter for projColumnList.
     *
     * @return value for projColumnList
     */
    @XmlElement
    public ProjColumnsConverter getProjColumnList() {
        if (expandLevel > 0) {
            if (entity.getProjColumnList() != null) {
                return new ProjColumnsConverter(entity.getProjColumnList(), uri.resolve("projColumnList/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for projColumnList.
     *
     * @param value the value to set
     */
    public void setProjColumnList(ProjColumnsConverter value) {
        entity.setProjColumnList((value != null) ? new java.util.ArrayList<ProjColumn>(value.getEntities()) : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the Project entity.
     *
     * @return an entity
     */
    @XmlTransient
    public Project getEntity() {
        if (entity.getProjId() == null) {
            ProjectConverter converter = UriResolver.getInstance().resolve(ProjectConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved Project entity.
     *
     * @return an resolved entity
     */
    public Project resolveEntity(EntityManager em) {
        List<ProjColumn> projColumnList = entity.getProjColumnList();
        List<ProjColumn> newprojColumnList = new java.util.ArrayList<ProjColumn>();
        if (projColumnList != null) {
            for (ProjColumn item : projColumnList) {
                newprojColumnList.add(em.getReference(ProjColumn.class, item.getId()));
            }
        }
        entity.setProjColumnList(newprojColumnList);
        return entity;
    }
}
